<?php
namespace app\controller\handler;
use app\controller\Common;
use think\Db;
use think\Model;

class User extends Common{

  public function getAccountList(){
    $base = input('post.');
    $page = isset($base['page']) ? $base['page'] : 1;
    $pagesize = isset($base['pagesize']) ? $base['pagesize'] : 10;
    $current = ($page-1)*$pagesize;
    $projectId = model('User')::getCurrentProjectId();
    $cond = $projectId != 0 ? " and p.id=$projectId " : " ";
    $sql = "select u.*, pr.name role_name, p.name project_name
            from `#__user` u
            left join `#__project_role` pr on pr.id=u.role_id and pr.status=1
            left join `#__project` p on p.id=pr.project_id and p.status=1
            where u.status=1 and pr.name!='超级管理员' $cond order by u.id desc limit ?,?
          ";
    $countSql = "select count(*) as total
            from `#__user` u
            left join `#__project_role` pr on pr.id=u.role_id and pr.status=1
            left join `#__project` p on p.id=pr.project_id and p.status=1
            where u.status=1 and pr.name!='超级管理员' $cond order by u.id desc
          ";
    $res = Db::query($sql,[$current,$pagesize]);
    $count = Db::query($countSql);
    if($res && count($res)>0){
      $result['ret'] = 1;
      $result['data'] = $res;
      $result['total'] = $count[0]['total'];
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function getAccountDetailById(){
    $wh['id'] = input('post.')['id'];
    $wh['status'] = 1;
    $res = model('User')::get($wh);
    if($res){
      $result['ret'] = 1;
      $result['data'] = $res;
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function addAccount(){
    $base = input('post.')['base'];
    $base['type'] = 2;
    $base['ctime'] = time();
    $base['password'] = base_encode($base['password']);
    $base['cover'] = '/static/img/index/ceo.jpg';
    $res = model('User')::create($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '添加成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '添加失败';
    }
    return $result;
  }

  public function editAccount(){
    $base = input('post.')['base'];
    $wh['id'] = input('post.')['id'];
    $res = model('User')::where($wh)->update($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '编辑成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '编辑失败';
    }
    return $result;
  }

  public function deleteAccount(){
    $wh['id'] = input('post.')['base']['id'];
    $up['status'] = 0;
    $res = model('User')::where($wh)->update($up);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '删除成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '删除失败';
    }
    return $result;
  }

  public function getPositionList(){
    $base = input('post.');
    $page = isset($base['page']) ? $base['page'] : 1;
    $pagesize = isset($base['pagesize']) ? $base['pagesize'] : 10;
    $current = ($page-1)*$pagesize;
    $projectId = model('User')::getCurrentProjectId();
    $cond = $projectId != 0 ? " and p.id=$projectId " : " ";
    $sql = "select pr.*, p.name project_name, p.root
            from `#__project_role` pr
            left join `#__project` p on p.id=pr.project_id and p.status=1
            where pr.status=1 and pr.name!='超级管理员' $cond order by pr.id desc limit ?,?
          ";
    $countSql = "select count(*) as total
            from `#__project_role` pr
            left join `#__project` p on p.id=pr.project_id and p.status=1
            where pr.status=1 and pr.name!='超级管理员' $cond order by pr.id desc
          ";
    $res = Db::query($sql,[$current,$pagesize]);
    $count = Db::query($countSql);
    if($res && count($res)>0){
      $result['ret'] = 1;
      $result['data'] = $res;
      $result['total'] = $count[0]['total'];
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function getPositionDetailById(){
    $wh['id'] = input('post.')['id'];
    $wh['status'] = 1;
    $res = model('ProjectRole')::get($wh);
    if($res){
      $result['ret'] = 1;
      $result['data'] = $res;
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function addPosition(){
    $base = input('post.')['base'];
    $base['project_id'] = model('User')::getCurrentProjectId();
    $res = model('ProjectRole')::create($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '添加成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '添加失败';
    }
    return $result;
  }

  public function editPosition(){
    $base = input('post.')['base'];
    $wh['id'] = input('post.')['id'];
    $res = model('ProjectRole')::where($wh)->update($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '编辑成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '编辑失败';
    }
    return $result;
  }

  public function deletePosition(){
    $wh['id'] = input('post.')['base']['id'];
    $up['status'] = 0;
    $res = model('ProjectRole')::where($wh)->update($up);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '删除成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '删除失败';
    }
    return $result;
  }


}//END